package com.file.system.plugin.shiro;

import com.file.system.entity.system.WebUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;


/**
 * shiro工具类
 *
 * @date
 */
public class ShiroTools {

    /**
     * 获取当前登录用户
     *
     * @date
     */
    public static WebUser getShiroUser() {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        WebUser user = (WebUser) subject.getPrincipal();
        return user;
    }


    /**
     * 获取登录用户
     */
    public static Long getId() {
        //获取登录用户
        Subject subject = SecurityUtils.getSubject();
        WebUser user = (WebUser) subject.getPrincipal();
        return user != null ? user.getId() : null;
    }


    /**
     * 获取用户名
     */
    public static String getName() {
        //获取安全工具
        Subject subject = SecurityUtils.getSubject();
        //获取用户
        WebUser user = (WebUser) subject.getPrincipal();
        //返回用户名
        return user != null ? user.getName() : null;
    }

    /**
     * 获取Session
     *
     * @date
     */
    public static Session getSession() {
        //获取Subject
        Subject subject = SecurityUtils.getSubject();
        //获取Session
        Session session = subject.getSession();
        //返回Session
        return session;
    }


    /**
     * 获取HttpServletResponse对象
     */
    public static HttpServletResponse getHttpResponse(ServletResponse response) {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        //设置响应类型
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        //设置编码
        httpServletResponse.setCharacterEncoding("UTF-8");
        return httpServletResponse;
    }

}
